<!-- 品牌管理 -->
<template>
    <div>
        
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                     <a-col :md="8">
                        <a-form-item label="等级">
                            <a-select v-model="grade" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in gradeList" :key="i" :value="item.grade">{{item.gradeName}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button type="primary" @click="getData()">查询</a-button>
                            <a-button style="margin-left: 20px" icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table size="small" rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" >
              <template slot="limitGrade" slot-scope="text, record">
                    <span v-for="(items,i) in gradeList" :key="i">
                        <span v-if="items.grade == record.limitGrade">{{items.gradeName}}</span>
                    </span>
                    <span v-for="(items,i) in gradeTypeList" :key="i">
                        <a-tag  v-if="items.id == record.gradeType" >{{items.name}}</a-tag>  
                    </span>
                </template>
                 <template slot="status" slot-scope="text, record">
                       <a-switch checked-children="是" un-checked-children="否" disabled :checked="record.status == '1' ? true : false" />
               </template>
            <template slot="operation" slot-scope="text, record">
                
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <a-modal
            title="添加、修改提现设置"
            :visible="visible"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                   
                     <a-form-item label="限制等级">
                        <a-select  style="width:130px" v-model="item.limitGrade" placeholder="请选择" allowClear>
                            <a-select-option v-for="(item,i) in gradeList" :key="i" :value="item.grade">{{item.gradeName}}</a-select-option>
                        </a-select>-
                         <a-select style="width:130px" v-model="item.gradeType" placeholder="请选择" allowClear>
                            <a-select-option v-for="(item,i) in gradeTypeList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                   
                     <a-form-item label="单笔最小金额">
                        <a-input   v-model="item.singleMinAmount" placeholder="请输入单笔最小金额" allow-clear></a-input>
                    </a-form-item>
                     <a-form-item label="单笔最大金额">
                        <a-input   v-model="item.singleMaxAmount" placeholder="请输入单笔最大金额" allow-clear></a-input>
                    </a-form-item>
                     <a-form-item label="单日最大金额">
                        <a-input   v-model="item.everyDayMaxAmount" placeholder="请输入单日最大金额" allow-clear></a-input>
                    </a-form-item>
                     <a-form-item label="是否开启">
                        <a-radio-group v-model="item.status" >
                            <a-radio  v-for="(item,i) in isHideList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                   
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryWithdrawConfig,updateWithdrawConfig  } from '@/api/product';
import {  queryGradeProduct } from '@/api/product';
import { columns } from './table';
import moment from "moment";
export default {
    data () {
        return {
             brandId: this.global.brandId,
            dataList: [],
            total: 0,
            size: 10,
            page: 1,
            columns,
            isload: false,
            visible: false,
            item: {
                id: '',
                 singleMinAmount:"",//单笔最小金额		
                singleMaxAmount:"",//单笔最大金额
                everyDayMaxAmount:"",//单日最大金额		
                status:'1',//是否开启,0:否,1:是	
                gradeType:'0',
                limitGrade:"",
            },
            gradeTypeList: [
                { id: '0', name: '大于'},
                { id: '1', name: '小于'},
                { id: '2', name: '等于'},
                { id: '3', name: '大于等于'},
                { id: '4', name: '小于等于'},
                
            ],
            isHideList: [
                { id: '1', name: '是'},
                { id: '0', name: '否'},
            ],
            grade: undefined,
            gradeList: [],
               previewImgVisible: false
        };
    },
     async created(){
        try {
            this.getAllData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
   
    // created(){
    //     this.getBrand()
    //     this.getData()
    // },
    methods: {
        moment,
         async getAllData(){
            try {
                this.gradeList = await this.getGrade()
                    this.getData()
                } catch (error) {
                    this.$notification['error']({
                        message: '提示',
                        description: '网路丢失，请稍后再试！',
                        duration: 8,
                    })
                }
        },
         // 查询等级信息
        getGrade(){
            let _this = this
            return new Promise((resolve,reject)=>{
                const params = {
                    'brandId':_this.brandId,
                    'size':99,
                    'page':0,
                }
                queryGradeProduct(params)
                .then(res=>{
                    if(res.success){
                        resolve(res.data.content)
                    }else{
                        reject()
                    }
                })
            })
        },
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'grade':_this.grade,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryWithdrawConfig(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){

            this.item={
                id: '',
                singleMinAmount:"",//单笔最小金额		
                singleMaxAmount:"",//单笔最大金额
                everyDayMaxAmount:"",//单日最大金额		
                status:'1',//是否开启,0:否,1:是	
                gradeType:'0',
                limitGrade:"",
            },
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
          this.visible = true
          
        },
        // 确定添加、删除
        handleOk(){
          
            let _this = this
            const params = {
                'id':_this.item.id,
                'brandId':_this.brandId,
                'limitGrade':_this.item.limitGrade,
                'gradeType':_this.item.gradeType,
                'singleMinAmount':_this.item.singleMinAmount,
                'singleMaxAmount':_this.item.singleMaxAmount,
                'everyDayMaxAmount':_this.item.everyDayMaxAmount,
                'status':_this.item.status,
            }
            if(!_this.brandId){
                this.$message.warning('请先选择品牌!')
                return
            }
            if(_this.item.limitGrade===''){
                this.$message.warning('请先提现等级!')
                return
            }
            if(_this.item.singleMinAmount<0){
                this.$message.warning('请先输入单笔最小金额!')
                return
            }
             if(_this.item.singleMaxAmount<0){
                this.$message.warning('请先输入单笔最大金额!')
                return
            }
           if(_this.item.everyDayMaxAmount<0){
                this.$message.warning('请先输入单日最大金额!')
                return
            }
            updateWithdrawConfig(params)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                    _this.visible = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id':item.id,
                'isDelete':1,
            }
            updateWithdrawConfig(params)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>